﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace ExtendBLL
{
    public class UserRole
    {
        /// <summary>
        /// 根据UserID找出已经赋给此用户的角色信息
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public List<LabMS.Model.RoleTable> GetUserRoleByUserID(int UserID)
        {
            LabMS.BLL.UserRole BUR = new LabMS.BLL.UserRole();
            List<LabMS.Model.UserRole> listMUR = new List<LabMS.Model.UserRole>();

            #region 用户是否存在
            string str1 = "UserID="+UserID;
            LabMS.BLL.UserTable BUT = new LabMS.BLL.UserTable();
            List<LabMS.Model.UserTable> list = new List<LabMS.Model.UserTable>();
            list = BUT.GetModelList(str1);
            if (list.Count == 0)
            {
                throw(new Exception("1"));//用户不存在
            }
            #endregion

            #region 用户是否有角色
            string str = "UserID="+UserID;
            listMUR=BUR.GetModelList(str);//在UserRole表中找出RoleID

            if (listMUR.Count == 0)
            {
                throw(new Exception("2"));//用户没有对应角色
            }
            #endregion

            LabMS.BLL.RoleTable BRT = new LabMS.BLL.RoleTable();
            List<LabMS.Model.RoleTable> listMRT = new List<LabMS.Model.RoleTable>();

            int RoleID;
            string sqlstr = "";
            for (int i = 0; i < listMUR.Count; i++)
            {
                RoleID =int.Parse(listMUR[i].RoleID.Value.ToString());
                if (i == listMUR.Count - 1)
                {
                    sqlstr += " RoleId=" + RoleID;
                }
                else
                {
                    sqlstr += " RoleId="+RoleID+" or ";
                }
            }
            listMRT = BRT.GetModelList(sqlstr);
            return listMRT;
        }

        /// <summary>
        /// 根据UserID获得没有被分配过的角色
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public List<LabMS.Model.RoleTable> GetRoleTableExpect(int UserID)
        {
            LabMS.BLL.UserRole BUR = new LabMS.BLL.UserRole();
            List<LabMS.Model.UserRole> list = new List<LabMS.Model.UserRole>();
            string str = "UserID="+UserID;
            list = BUR.GetModelList(str);

            LabMS.BLL.RoleTable BRT = new LabMS.BLL.RoleTable();
            List<LabMS.Model.RoleTable> listRoleT = new List<LabMS.Model.RoleTable>();

            string strsql = "";
            int RoleID;
            if (list.Count == 0)//没有分配角色
            {
                strsql = "1=1";
            }
            else
            {
                for (int i = 0; i < list.Count; i++)
                {
                    RoleID =int.Parse(list[i].RoleID.Value.ToString());
                    if (i == list.Count - 1)
                    {
                        strsql += " RoleID!=" + RoleID;
                    }
                    else
                    {
                        strsql += " RoleID!="+RoleID+" and ";
                    }
                }                
            }
            listRoleT = BRT.GetModelList(strsql);
            return listRoleT;
        }

        /// <summary>
        /// 根据userid,roleid从userrole表中删除对应记录
        /// </summary>
        /// <param name="RoleID"></param>
        /// <param name="UserID"></param>
        public void DeleteByRoleIDUserID(int RoleID, int UserID)
        {
            string str = " RoleID=" + RoleID + " and UserID="+UserID+"";
            LabMS.BLL.UserRole BUR = new LabMS.BLL.UserRole();
            int ID = BUR.GetModelList(str)[0].ID;
            BUR.Delete(ID);
        }

        /// <summary>
        /// 根据RoleId查找出所有拥有该角色的用户
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public DataSet GetUserInfoByRoleID(int RoleId)
        {
            LabMS.BLL.UserRole BUR = new LabMS.BLL.UserRole();
            List<LabMS.Model.UserRole> list = new List<LabMS.Model.UserRole>();
            string str = "RoleID=" + RoleId;
            list = BUR.GetModelList(str);

            LabMS.BLL.UserTable BUserTable = new LabMS.BLL.UserTable();
            DataSet ds = new DataSet();
            int UserID;
            string strsql = "";
            if (list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    UserID = int.Parse(list[i].UserID.Value.ToString());
                    if (i == list.Count - 1)
                    {
                        strsql += " UserID=" + UserID;
                    }
                    else
                    {
                        strsql += " UserID=" + UserID + " or ";
                    }
                }
            }
            else
            {
                throw(new Exception("2"));//没有用户拥有该角色
            }
            ds=BUserTable.GetList(strsql);
            return ds;
        }
    }
}
